package cn.hit.tianqiao.dao.impl;

import cn.hit.tianqiao.dao.HotelDao;
import cn.hit.tianqiao.domain.Hotel;
import cn.hit.tianqiao.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class HotelDaoImpl implements HotelDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public List<Hotel> findWithinDistance(double longitude, double latitude, double distance) {
        String sql = "SELECT cid, cname, address, " +
                "ST_X(location) AS longitude, " + // Extracts the X coordinate
                "ST_Y(location) AS latitude, " +  // Extracts the Y coordinate
                "consphone, image_path, xc_path, " +
                "ST_Distance_Sphere(Point(?, ?), location) AS distance " +
                "FROM tab_hotel " +
                "WHERE ST_Distance_Sphere(Point(?, ?), location) <= ?";

        return template.query(sql,
                new BeanPropertyRowMapper<>(Hotel.class),
                longitude, latitude, longitude, latitude, distance);
    }
}